package com.li.aspect;

import com.alibaba.dubbo.config.annotation.Reference;
import com.li.entity.Result;
import com.li.pojo.Log;
import com.li.service.LogService;
import com.li.utils.ThreadLocalUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Aspect
public class LogAspect {

    @Reference
    private LogService logService;

    /**
     *  在web.xml中配置RequestContextListener, 就可以从ioc容器中获取request
     */
    @Autowired
    private HttpServletRequest request;

    public Object log(ProceedingJoinPoint pjp) {
        try {
            // 获取request
            /*HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();*/
            // 获取ip
            String ip = request.getRemoteAddr();
            // 获取操作时间
            Date date = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String logTime = sdf.format(date);
            //System.out.println(logTime);
            // 获取全限定类名
            String className = pjp.getTarget().getClass().getName();
            //System.out.println(className);
            // 获取执行的方法
            String methodName = pjp.getSignature().getName();
            //System.out.println(methodName);
            // 获取URL
            String url = request.getRequestURL().toString();
            //System.out.println(url);
            String uri = request.getRequestURI();
            //System.out.println(uri);
            // 获取用户名
            User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            String username = user.getUsername();
            //System.out.println(username);
            // 耗时
            long start = System.currentTimeMillis();
            Object proceed = pjp.proceed(pjp.getArgs());
            long end = System.currentTimeMillis();
            long time = end - start;
            //System.out.println(time);
            // 获取方法参数
            String args = "";
            Map<String, String[]> parameterMap = request.getParameterMap();
            for (Map.Entry<String, String[]> entry : parameterMap.entrySet()) {
                String key = entry.getKey();
                String[] value = entry.getValue();
                args = key + "=" + Arrays.toString(value);
            }

            Log log = new Log();
            log.setUsername(username);
            log.setLogTime(logTime);
            log.setIp(ip);
            log.setUrl(url);
            log.setTime(String.valueOf(time) + "ms");
            log.setUri(uri);
            log.setArgs(args);

            logService.save(log);

            return proceed;
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            return new Result(false, "");
        }
    }
}
